//
//  main.c
//  9 chapter
//
//  Created by edz on 2020/6/30.
//  Copyright © 2020 edz. All rights reserved.
//

#include <stdio.h>


#if 0
// 练习9-1
int main(void)
{
    char str[] = "ABC\0DEF";

    printf("字符串str为\"%s\"。\n", str);

    return 0;
}
#endif


#if 0
// 练习9-2
int main(){
    char s[] = "ABC";
    s[0] = 0;
    printf("%s",s);
    return 0;
}
#endif



#if 0
// 练习9-3
 
#include <string.h>
 
#define NUMBER 5
 
int main(void)
{
    int i;
    char s[NUMBER][128];
    
    for (i = 0; i < NUMBER; i++) {
        printf("s[%d]：", i);
        scanf("%s", s[i]);
        if (strcmp(s[i], "$$$$$") == 0)
            break;
    }
    
    for(i = 0; i < NUMBER; i++){
        if (strcmp(s[i], "$$$$$") == 0)
            break;
        printf("s[%d] = \"%s\"\n", i, s[i]);
    }
    
    return 0;
 }
#endif



#if 0
//练习9-4(使字符串为空字符串)
void null_string(char s[]){
    s[0] = 0;
}
#endif


#if 0
// 练习9-5
int str_char(const char s[],int c){
    int n=0;
    while (s[n]) {
        if (s[n] == c) {
            return n;
        }
        n++;
    }
    return -1;
}

int main(void)
{
    char c = 'o';
    char s[] = "helloworld";
    
    printf("字符c在字符串%s中的下标为：%d\n", s, str_char(s,c));
    
    return 0;
}
#endif


#if 0
// 练习9-6
int str_chnum(const char s[],int c){
    int n = 0;
    int count = 0;
    while (s[n]) {
        if (s[n] == c) {
            count++;
        }
        n++;
    }
    return count;
}

int main(void)
{
    char c = 'l';
    char s[] = "hellochina";
    
    printf("字符c在字符串%s中的有%d个\n", s, str_chnum(s, c));
    
    return 0;
}
#endif



#if 0
// 练习9-7
/*---编写函数，使字符串s显示n次。---*/

void put_stringn(const char s[], int n){
    int num, i = 0;
    
    for (num = 0; num < n; num++){
        //printf("%s\n",s);
        while(s[i]){
            putchar(s[i]);
            i++;
        }
        i = 0;
    }
}
 
int main(void)
{
    char s[] = "abc";
    
    int n = 3;
    
    put_stringn(s, n);
    printf("\n");
    
    return 0;
}
#endif


#if 1
// 练习9-8
/*---编写函数，实现字符串的逆向输出。---*/
 
#include <stdio.h>
#include <string.h>
void put_stringr(const char s[]){
    int num = 0, i = 0;
    while (s [num])
        num++;
    //num = (int)strlen(s);
    while (i < num){
        putchar(s[num - i -1]);
        i++;
    }
}
 
int main(void)
{
    char s [] = "hello";
    
    put_stringr(s);
    printf("\n");
    
    return 0;
}
#endif



#if 0
// 练习9-9
/*---逆向显示字符串s的字符---*/
 
#include <stdio.h>
 
void rev_string(char s[]){
    int num = 0, i = 0, temp;
    while (s [num])
        num++;
    for(i = 0; i < num/2; i++){
        temp = s[i];
        s[i] = s[num - i -1];
        s[num - i -1] = temp;
    }
    printf("%s",s);
    printf("\n");
}
 
int main(void)
{
    char s[] = "hello";
    
    rev_string(s);
    return 0;
 }
#endif



#if 0
// 练习9-10
/*---将字符串s中的数字字符全部删除。---*/
 
void del_digit(char s[]) {
    int i=0,temp;
    while (s[i]) {
        if (s[i] >= '0' && s[i] <= '9') {
            temp = i;
            while (s[temp]) {
                s[temp] = s[temp + 1];
                temp++;
            }
            i--;
        }
        i++;
    }
    printf("%s",s);
    printf("\n");
}

int main(){
    char s[] = "abc123ab12c";
    del_digit(s);
    return 0;
}
#endif

